<template>
  <div class="app-container">

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
    >
      <el-table-column label="序号" align="center" fixed width="200px">
        <template slot-scope="{row}">
          <span>{{ row.ruleId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="栏目" align="center" width="300px">
        <template slot-scope="{row}">
          <span>{{ row.ruleKey }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center">
        <template slot-scope="{row}">
          <span>{{ row.ruleValue }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="300px">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showRuleList" />

    <el-dialog :title="dialogTitleName" :visible.sync="dialogFormVisible" width="35%">
      <el-form ref="dataForm" :model="temp" label-width="100px" style="width: 80%; margin-left:5%;">
        <el-form-item label="编辑值" prop="experience">
          <el-input v-model="temp.ruleValue" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveData">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { showRuleList, saveRule } from '@/api/rules-settings'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'

export default {
  name: 'Settings',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20
      },
      temp: {
        ruleId: undefined,
        ruleKey: '',
        ruleValue: ''
      },
      dialogFormVisible: false,
      dialogTitleName: ''
    }
  },
  created() {
    this.showRuleList()
  },
  methods: {
    showRuleList() {
      this.listLoading = true
      showRuleList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showRuleList()
    },
    resetDialog() {
      this.temp = {
        ruleId: undefined,
        ruleKey: '',
        ruleValue: ''
      }
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogTitleName = '编辑 ' + row.ruleKey
      this.dialogFormVisible = true
    },
    saveData() {
      saveRule(this.temp).then(() => {
        this.showRuleList()
        this.dialogFormVisible = false
        this.$notify({
          title: 'Success',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>
